package com.zheng.study.company.dao.impl;

import com.zheng.study.base.bean.BaseEnumManager;
import com.zheng.study.base.dao.impl.BaseDaoImpl;
import com.zheng.study.company.dao.DutyDao;
import com.zheng.study.company.entity.Duty;
import com.zheng.study.company.entity.Power;
import com.zheng.study.company.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/11/13 。
 * ============================================================================
 */
@Repository
public class DutyDaoImpl extends BaseDaoImpl<Duty,String> implements DutyDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Duty> getUserDutyList(User user) {
		String hql = "from " + Duty.class.getName() + " d where d.user = :user and d.state = :state order by d.power.sortNo asc";
		return getSession().createQuery(hql).setParameter("user", user).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Duty getDutyByUserAndPower(User user, Power power) {
		String hql = "from " + Duty.class.getName() + " d where d.state = :state and d.user = :user and d.power = :power";
		List<Duty> dutyList = getSession().createQuery(hql)
				.setParameter("state", BaseEnumManager.StateEnum.Enable)
				.setParameter("user", user)
				.setParameter("power", power)
				.list();
		return dutyList.size()>0 ? dutyList.get(0) : null;
	}

}
